package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportUserLoginLogRpcService;
import com.hunttown.mes.rpc.domain.TransportUserLoginLogDTO;
import com.hunttown.mes.rpc.domain.query.TransportUserLoginLogDTOQuery;
import com.hunttown.mes.service.TransportUserLoginLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.TransportUserLoginLogConverts.convert;

/**
 * created by wangjunfu on 2019-07-06 10:59:06
 */
@Service
public class TransportUserLoginLogLogRpcServiceImpl implements TransportUserLoginLogRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportUserLoginLogLogRpcServiceImpl.class);

    private final TransportUserLoginLogService transportUserLoginLogService;

    @Autowired
    public TransportUserLoginLogLogRpcServiceImpl(TransportUserLoginLogService transportUserLoginLogService) {
        this.transportUserLoginLogService = transportUserLoginLogService;
    }

    @Override
    public TransportUserLoginLogDTO insert(TransportUserLoginLogDTO transportUserLoginLogDTO) {
        try {
            return convert(transportUserLoginLogService.insert(convert(transportUserLoginLogDTO)));
        } catch (Exception e) {
            logger.error("TransportUserLoginLog create failed.", e);
            throw new RuntimeException("TransportUserLoginLog create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportUserLoginLogDTO transportUserLoginLogDTO) {
        try {
            return transportUserLoginLogService.updateInfoById(convert(transportUserLoginLogDTO));
        } catch (Exception e) {
            logger.error("TransportUserLoginLog update by id failed.", e);
            throw new RuntimeException("TransportUserLoginLog update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportUserLoginLogDTOQuery transportUserLoginDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportUserLoginDTOQuery.getW_id() != null && transportUserLoginDTOQuery.getW_id() > 0) {
                query.put("w_id", transportUserLoginDTOQuery.getW_id());
            }

            if (transportUserLoginDTOQuery.getIds() != null && !transportUserLoginDTOQuery.getIds().equals("")) {
                query.put("ids", transportUserLoginDTOQuery.getIds());
            }

            return transportUserLoginLogService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportUserLoginLog update by query failed.", e);
            throw new RuntimeException("TransportUserLoginLog update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportUserLoginLogDTO selectById(Integer id) {
        try {
            return convert(transportUserLoginLogService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportUserLoginLog select by id failed.", e);
            throw new RuntimeException("TransportUserLoginLog select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportUserLoginLogDTO selectObjByQuery(TransportUserLoginLogDTOQuery transportUserLoginDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            return convert(transportUserLoginLogService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportUserLoginLog select obj by query failed.", e);
            throw new RuntimeException("TransportUserLoginLog select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportUserLoginLogDTO> getListForPage(TransportUserLoginLogDTOQuery transportUserLoginDTOQuery) {
        Query query = Query.page(transportUserLoginDTOQuery.getPn(), transportUserLoginDTOQuery.getPageSize());

        if (transportUserLoginDTOQuery.getId() != null && transportUserLoginDTOQuery.getId() > 0) {
            query.put("id", transportUserLoginDTOQuery.getId());
        }

        if (transportUserLoginDTOQuery.getIds() != null && !transportUserLoginDTOQuery.getIds().equals("")) {
            query.put("ids", transportUserLoginDTOQuery.getIds());
        }

        if (transportUserLoginDTOQuery.getClientType() != null && transportUserLoginDTOQuery.getClientType() != -9999) {
            query.put("clientType", transportUserLoginDTOQuery.getClientType());
        }

        if (transportUserLoginDTOQuery.getUserId() != null && transportUserLoginDTOQuery.getUserId() > 0) {
            query.put("userId", transportUserLoginDTOQuery.getUserId());
        }

        if (StringUtils.isNotBlank(transportUserLoginDTOQuery.getLikeName())) {
            query.put("likeName", transportUserLoginDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(transportUserLoginDTOQuery.getShowDate())) {
            query.put("showDate", transportUserLoginDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportUserLoginDTOQuery.getStartDate())) {
            query.put("startDate", transportUserLoginDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportUserLoginDTOQuery.getEndDate())) {
            query.put("endDate", transportUserLoginDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(transportUserLoginDTOQuery.getOrderBy())) {
            query.put("orderBy", transportUserLoginDTOQuery.getOrderBy());
        }

        //区间日期
        if (StringUtils.isNotBlank(transportUserLoginDTOQuery.getDaterange())) {
            String[] date = transportUserLoginDTOQuery.getDaterange().split("~");
            if (date.length == 2) {
                query.put("startDate", date[0].trim());
                query.put("endDate", date[1].trim());
            }
        }

        List<TransportUserLoginLogDTO> list = convert(transportUserLoginLogService.selectByQuery(query));
        Page<TransportUserLoginLogDTO> page = Page.from(transportUserLoginDTOQuery.getPn(), transportUserLoginDTOQuery.getPageSize(), list);
        page.setTotalItems(transportUserLoginLogService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportUserLoginLogService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportUserLoginLog delete failed.", e);
            throw new RuntimeException("TransportUserLoginLog delete failed, Please order your code.", e);
        }
    }
}